/*******************************************************************************

This code file estimates New York City's rank-rank mobility function and produces
Figure A22, "Fitted Intergenerational Income Rank--Rank Function for New York City."

*******************************************************************************/

*** Manage settings

	run "$dir/code/modules/settings.do"
	
********************************************************************************
* Make graph and rank-rank function
********************************************************************************

*** Load data

	import excel "/Users/esoltas/Dropbox (MIT)/Research/NYC421a/data/raw/NYC_rankrank_mobility.xlsx", sheet("Sheet1") cellrange(B9:D109) firstrow clear
	
	replace s_kir_pooled_pooled_nyc = 100 * s_kir_pooled_pooled_nyc
	replace s_kir_pooled_pooled = 100 * s_kir_pooled_pooled
	
*** Impute NYC values

	reg s_kir_pooled_pooled_nyc s_kir_pooled_pooled c.s_kir_pooled_pooled#c.s_kir_pooled_pooled
	predict nyc_kir_pooled_pooled_pred
	
*** Save outputs

	* Graph
	tw scatter s_kir_pooled_pooled_nyc par_pctile, mcolor(black) || ///
		line s_kir_pooled_pooled par_pctile, lcolor(maroon) || ///
		line nyc_kir_pooled_pooled_pred par_pctile, lcolor(navy) ///
		legend(order(1 "NYC, Actual" 2 "National, Actual" 3 "NYC, Fitted") rows(1)) ///
		graphregion(color(white)) xtitle("Parent Household Income Rank") ///
		ytitle("Child Individual Income Rank") ylabel(,nogrid) 
		
	graph export "$figs/nyc_rankrank_refit.pdf", replace
	graph export "$figs_overleaf/nyc_rankrank_refit.pdf", replace
	
	* Dataset
	keep nyc_kir_pooled_pooled_pred par_pctile
	save "$code/modules/HendrenSprungKeyser/Welfare/new_programs/nyc_parent_child_rank.dta", replace
